<template>
  <div class="tools-div">
    <el-button type="success" size="small" @click="addShow">添 加</el-button>
  </div>

  <el-dialog v-model="dialogVisible" :title="dialogTitle" width="30%">
    <el-form label-width="120px">
      <el-form-item label="菜单标题">
        <el-input v-model="sysMenu.title"/>
      </el-form-item>
      <el-form-item label="路由名称">
        <el-input v-model="sysMenu.component"/>
      </el-form-item>
      <el-form-item label="排序" >
        <el-input v-model="sysMenu.sortValue"/>
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="sysMenu.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="0">停用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="上层菜单">
      <el-tree
        :data="list"
        ref="tree"
        :highlight-current ="true"
        :check-strictly ="true"
        :default-expand-all = "false"
        :check-on-click-node="false"
        node-key="id"
        :props="defaultProps"
      />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveOrUpdate">提交</el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
  <el-table
    :data="list"
    style="width: 100%; margin-bottom: 20px"
    row-key="id"
    border
    :default-expand-all = "false"
  >
    <el-table-column prop="title" label="菜单标题" />
    <el-table-column prop="component" label="路由名称" />
    <el-table-column prop="sortValue" label="排序" />
    <el-table-column prop="status" label="状态" #default="scope">
      {{ scope.row.status == 1 ? '正常' : '停用' }}
    </el-table-column>
    <el-table-column prop="createTime" label="创建时间" />

    <el-table-column label="操作" align="center" width="280" #default="scope" >
      <el-button type="primary" size="small" @click="editShow(scope.row)">修改</el-button>
      <el-button type="danger" size="small" @click="remove(scope.row.id)">删除</el-button>
    </el-table-column>
  </el-table>
</template>

<script setup>
//引入调用的方法
import { ref , onMounted } from "vue"
import { ElMessage, ElMessageBox } from 'element-plus'
import { findNodes,saveOrUpdateMenu,deleteMenu } from "@/api/system/menu";


onMounted(() => {
  getfindNodes()
})

// 定义表格数据模型
const list = ref([])
const tree = ref()
const defaultProps = {
  children: 'children',
  label: 'title',
}

//页面表单数据
const defaultForm = {
  id: '',
  parentId: 0,
  title: '',
  url: '',
  component: '',
  icon: '',
  sortValue: 1,
  status: 1,
}
// 表单响应式数据模型对象
const sysMenu = ref(defaultForm)
const dialogVisible = ref(false)
const dialogTitle = ref('')

const addShow = () => {
  dialogTitle.value = '添加菜单'
  dialogVisible.value = true
}
const editShow = (row) => {
  dialogTitle.value = '修改菜单'
  dialogVisible.value = true
  sysMenu.value = row
}
//=======================修改或者新增功能====================
const saveOrUpdate = async () => {
  //获取el tree 选中的值id值
  sysMenu.value.parentId = tree.value.getCurrentNode().id
  const {code} = await saveOrUpdateMenu(sysMenu.value);
  if (code == 200) {
    ElMessage.success("操作成功")
    dialogVisible.value = false
    getfindNodes()
  }
}
//=======================删除功能====================
const remove = async (id) => {
  ElMessageBox.confirm('此操作将永久删除该记录, 是否继续?', 'Warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    const { code , message } = await deleteMenu(id)
    if(code === 200) {
      ElMessage.success('删除成功')
      getfindNodes()
    }else {
      ElMessage.error(message)
    }
  })
}


//获取列表数据
const getfindNodes = async () =>{
     const {data} = await findNodes();
     list.value = data;
}


</script>

<style scoped>
.tools-div {
  margin: 10px 0;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}
</style>